package com.ruoyi.project.erp.goods.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.EmptyUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.domain.ZtreeString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.ruoyi.project.erp.goods.mapper.ErpGoodsMapper;
import com.ruoyi.project.erp.goods.domain.ErpGoods;
import com.ruoyi.project.erp.goods.service.IErpGoodsService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 物品Service业务层处理
 *
 * @author qinghuan
 * @date 2021-08-08
 */
@Service
@Slf4j
public class ErpGoodsServiceImpl implements IErpGoodsService {
    @Autowired
    private ErpGoodsMapper erpGoodsMapper;

    /**
     * 查询物品
     *
     * @param goodsId 物品主键
     * @return 物品
     */
    @Override
    public ErpGoods selectErpGoodsByGoodsId(String goodsId) {
        return erpGoodsMapper.selectErpGoodsByGoodsId(goodsId);
    }

    /**
     * 查询物品列表
     *
     * @param erpGoods 物品
     * @return 物品
     */
    @Override
    public List<ErpGoods> selectErpGoodsList(ErpGoods erpGoods) {
        return erpGoodsMapper.selectErpGoodsList(erpGoods);
    }

    /**
     * 新增物品
     *
     * @param erpGoods 物品
     * @return 结果
     */
    @Override
    public int insertErpGoods(ErpGoods erpGoods) {
        erpGoods.setCreateTime(DateUtils.getNowDate());
        return erpGoodsMapper.insertErpGoods(erpGoods);
    }

    /**
     * 修改物品
     *
     * @param erpGoods 物品
     * @return 结果
     */
    @Override
    public int updateErpGoods(ErpGoods erpGoods) {
        return erpGoodsMapper.updateErpGoods(erpGoods);
    }

    /**
     * 批量删除物品
     *
     * @param goodsIds 需要删除的物品主键
     * @return 结果
     */
    @Override
    public int deleteErpGoodsByGoodsIds(String goodsIds) {
        return erpGoodsMapper.deleteErpGoodsByGoodsIds(Convert.toStrArray(goodsIds));
    }

    /**
     * 删除物品信息
     *
     * @param goodsId 物品主键
     * @return 结果
     */
    @Override
    public int deleteErpGoodsByGoodsId(String goodsId) {
        return erpGoodsMapper.deleteErpGoodsByGoodsId(goodsId);
    }

    /**
     * 查询物品树列表
     *
     * @return 所有物品信息
     */
    @Override
    public List<ZtreeString> selectErpGoodsTree() {
        List<ErpGoods> erpGoodsList = erpGoodsMapper.selectErpGoodsList(new ErpGoods());
        List<ZtreeString> ztrees = new ArrayList<>();
        for (ErpGoods erpGoods : erpGoodsList) {
            ZtreeString ztree = new ZtreeString();
            ztree.setId(erpGoods.getGoodsId());
            ztree.setpId(erpGoods.getParentId());
            ztree.setName(erpGoods.getGoodName());
            ztree.setTitle(erpGoods.getGoodName());
            ztrees.add(ztree);

        }
        return ztrees;
    }


    /**
     * 根据项目id查询顶级物品列表
     *
     * @param projectId
     * @return
     */
    @Override
    public List<ErpGoods> selectParentGoodsByProjectId(Long projectId) {
        return erpGoodsMapper.selectParentGoodsByProjectId(projectId);
    }


    /**
     * 根据项目id查询物品汇总信息
     *
     * @param projectId
     * @return
     */
    @Override
    public ErpGoods selectTotalNumByProjectId(Long projectId) {
        return erpGoodsMapper.selectTotalNumByProjectId(projectId);
    }

    @Override
    public String importGoods(List<ErpGoods> goodsList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(goodsList) || goodsList.size() == 0) {
            throw new BusinessException("数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String operName = ShiroUtils.getLoginName();
        for (ErpGoods erpGoods : goodsList) {
            try {
                Thread.sleep(200);
                if (EmptyUtil.isEmpty(erpGoods.getGoodsId())) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：物品编码为空";
                    failureMsg.append(msg);
                } else if (EmptyUtil.isEmpty(erpGoods.getProjectId())) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：项目编码为空";
                    failureMsg.append(msg);
                } else if (StringUtils.isContainChinese(erpGoods.getGoodsId())) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：物品编码不能包含中文";
                    failureMsg.append(msg);
                } else if (erpGoods.getGoodsId().contains("/")) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：物品编码不能包含/";
                    failureMsg.append(msg);
                } else if (erpGoods.getGoodsId().contains("\n")) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：物品编码不能包含换行符";
                    failureMsg.append(msg);
                } else {
                    this.insertErpGoods(erpGoods);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、物品 " + erpGoods.getGoodName() + " 导入成功");
                }

            } catch (DuplicateKeyException e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：物品编码'" + erpGoods.getGoodsId() + "'已存在，请勿重复导入。";
                failureMsg.append(msg);
                log.error(msg, e);
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物品 " + erpGoods.getGoodName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /**
     * 通过项目删除物品
     *
     * @param projectId
     * @return
     */
    @Override
    public int removeAllByProjectId(Long projectId) {
        return erpGoodsMapper.removeAllByProjectId(projectId);

    }

    /**
     * 导出物品列表
     *
     * @param erpGoods
     * @return
     */
    @Override
    public List<ErpGoods> exportErpGoodsList(ErpGoods erpGoods) {
        return erpGoodsMapper.exportErpGoodsList(erpGoods);
    }
}
